﻿using IBLL;
using IDAL;
using Microsoft.AspNetCore.Http;
using Models;
using Models.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BLL
{
    public class OrderInfoBLL:IOrderInfoBLL
    {
        private UnmanContexts _dbContext;
        private IOrderInfoDAL _orderInfoDAL;
        private readonly IHttpContextAccessor _httpContextAccessor;
        public OrderInfoBLL(
            UnmanContexts dbContext
            ,IOrderInfoDAL orderInfoDAL
            , IHttpContextAccessor httpContextAccessor
            )
        {
            _dbContext = dbContext;
            _orderInfoDAL = orderInfoDAL;
            _httpContextAccessor = httpContextAccessor;
        }
        public List<GetOrderInfoDTO> GetOrderInfos(int page, int limit, string id, out int count)
        {
            var data = from o in _dbContext.OrderInfo
                           //链第一个表查询领导信息
                       join c in _dbContext.CommodityInfo.Where(u => u.IsDalete == false)
                       on o.ProductId equals c.Id
                       into TempDU
                       from uu in TempDU.DefaultIfEmpty()
                           
                       select new GetOrderInfoDTO
                       {
                           Id = o.Id,
                           ProductId = o.ProductId,
                           Description = o.Description,
                           CreateTime = o.CreateTime,
                           UserId=o.UserId,
                           UserName=o.UserName,
                           CommodityName = uu.CommodityName,
                           Money = o.Money

                       };
            //部门名称不能为空时拼接查询参数
            if (!string.IsNullOrWhiteSpace(id))
            {
                data = data.Where(u => u.Id.Contains(id));
            }

            //求总数
            count = data.Count();
            //分页
            var listPage = data.OrderByDescending(d => d.CreateTime).Skip(limit * (page - 1)).Take(limit).ToList();
            //返回结果
            return listPage;
        }

        #region 添加订单的方法（CreateUserInfo）
        /// <summary>
        /// 添加用户的方法
        /// </summary>
        /// <param name="entity">新订单的实体</param>
        /// <returns></returns>
        public bool CreateOrderInfo(OrderInfo entity , out string msg)
        {
            
            if (string.IsNullOrWhiteSpace(entity.ProductId))
            {
                msg = "商品Id不能为空！";
                return false;
            }
            
            //赋值id
            entity.Id = Guid.NewGuid().ToString();
            entity.CreateTime = DateTime.Now;
            HttpContext httpContext = _httpContextAccessor.HttpContext;
            string userId = httpContext.Request.Cookies["UserId"];
            string userName = httpContext.Request.Cookies["UserName"];
            if (userId != null)
            {
                entity.UserId = userId;
            }
            if (userName != null)
            {
                entity.UserName = userName;
            }
            //更新到数据库
            bool isSuccess = _orderInfoDAL.CreateEntity(entity);
            msg = isSuccess ? $"添加{entity.UserName}成功！" : "添加数据失败";
            return isSuccess;

        }
        #endregion

    }
}
